﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Common;
namespace OF.Web
{
    public partial class order : BuyerAdminPage
    {
        BLL.Order.Order bll_order = new BLL.Order.Order();
        DAL.BuyerAddress dal_address = new DAL.BuyerAddress();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] temp = Common.Cookie.getCookie("ordershop").Split('_');
                string type = temp[0];

                txtAddr.Visible = false;
                txtArriveTime.Visible = false;
                txtnum.Visible = false;

                if (type == "waimai") //外卖
                {
                    //绑定地址信息
                    var list = dal_address.GetModelList(" buyerid=" + WebUtils.GetBuyerId() + " and state !=0 order by state desc");
                    if (list != null)
                    {
                        txtAddr.Text = list[0].address;
                        txtAddr.Visible = true;
                    }
                }
                else
                {  //点餐
                    txtArriveTime.Text = DateTime.Now.ToString();
                    txtnum.Text = "1";
                    txtArriveTime.Visible = true;
                    txtnum.Visible = true;
                }
                txtphone.Text = WebUtils.GetBuyerName();

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Common.Cookie.getCookie("ordershop") == null)
            {
                Response.Redirect("/index.aspx");
                return;
            }
            string[] temp = Common.Cookie.getCookie("ordershop").Split('_');
            string type = temp[0];
            string sellerid = temp[1];

            string buyerid = WebUtils.GetBuyerId().ToString();
            string time = txtArriveTime.Text;
            string phone = txtphone.Text;
            string address = txtAddr.Text; //送餐地址
            string population = txtnum.Text;   //预约人数
            string remark = txtremark.Text;

            string dish = hfdishs.Value;
            string state="";

            dish = dish.EndsWith(",]") ? dish.Substring(0, dish.Length - 2) + "]" : dish;
            string result = bll_order.doAddOrder(sellerid, buyerid, dish, time, phone, address, population, remark, type, state);
            if (result == Enmus.RETURN.SUC.ToString())
            {
                MessageBox.ShowAndRedirect(this, "提交订单成功", "/user/orderlist.aspx");
                //清空缓存
                Common.Cookie.deleteCookie("dishcart");
                Common.Cookie.deleteCookie("ordershop");
            }
            else
            {
                MessageBox.Show(this, "提交订单失败");
            }
        }
    }
}